/**
 入驻小区
 **/
(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            allocationOrderInInfo: {
                productManages: [],
                resourceSuppliers: [],
                shopHouses: [],
                total: 0,
                records: 1,
                remark: '',
                srcShId: '',
                srcShName: '',
                targetShId: '',
                targetShName: '',
                shopId: '',
                shopName: '',
                count: '',
                productId: '',
                specId: '',
                targetShopId: '',
                targetShopName: '',
                totalMount: 0.00,
                moreCondition: false,
                communityId: vc.getCurrentCommunity().communityId
            }
        },
        _initMethod: function () {
            $that._listAllocationStorehouse();
        },
        _initEvent: function () {
            vc.on('allocationOrderIn', 'chooseProductAndSpec', function (_product) {
                    $that.allocationOrderInInfo.productManages.push(_product);
                    $that.allocationOrderInInfo.shopId = _product.shopId;
                    $that.allocationOrderInInfo.srcShId = _product.shId;
                    $that.allocationOrderInInfo.srcShName = _product.shName;
                    $that.allocationOrderInInfo.productId = _product.productId;
                    $that.allocationOrderInInfo.specId = _product.defaultSpecValue.specId;
                    $that._selectShopHouses(_product.shId);
                }
            );
        },
        methods: {
            _deleteProduct: function (_Product) {
                let _Products = $that.allocationOrderInInfo.productManages;
                let index = _Products.indexOf(_Product);
                if (index > -1) {
                    _Products.splice(index, 1);
                }
            },
            _openAllocationOrderInModel: function () {
                vc.emit('chooseProductAndSpec', 'openChooseProductModel', {});
            },
            _listAllocationStorehouse: function (_page, _rows) {
                var param = {
                    params: {
                        page: 1,
                        row: 100
                    }
                };
                //发送get请求
                vc.http.apiGet('resourceSupplier.listResourceSuppliers',
                    param,
                    function (json, res) {
                        var _resourceSupplierManageInfo = JSON.parse(json);
                        $that.allocationOrderInInfo.resourceSuppliers = _resourceSupplierManageInfo.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _goBack: function () {
                vc.goBack();
            },
            _submitApply: function () {
                //校验数据
                let _saveFlag = true;
                if ($that.allocationOrderInInfo.productManages.length < 1) {
                    vc.toast('请选择物品');
                    return;
                }
                if (!$that.allocationOrderInInfo.targetShId) {
                    vc.toast('请选择目标仓库');
                    return;
                }
                if ($that.allocationOrderInInfo.remark == '') {
                    vc.toast('申请说明不能为空');
                    return;
                }
                $that.allocationOrderInInfo.productManages.forEach(item => {
                        if (!item.count || item.count <= 0) {
                            vc.toast("请填写调拨数量");
                            _saveFlag = false;
                            return;
                        }
                        if (parseInt(item.count) > parseInt(item.stock)) {
                            vc.toast(item.prodName + "库存不足");
                            _saveFlag = false;
                            return;
                        }
                        $that.allocationOrderInInfo.count = item.count;
                    }
                );
                if (!_saveFlag) {
                    return;
                }
                vc.http.apiPost(
                    '/allocationOrder/saveAllocationOrder',
                    JSON.stringify($that.allocationOrderInInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        if (res.status == 200) {
                            var _json = JSON.parse(json);
                            //关闭model
                            vc.goBack();
                            vc.toast(_json.msg);
                            return;
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    }
                );
            },
            _caculTotalMount: function () {
                let j = 0;
                $that.allocationOrderInInfo.productManages.forEach(item => {
                    if (!isNaN(item.count) && !isNaN(item.price)) {
                        j += parseFloat(item.count) * parseFloat(item.price).toFixed(2);
                    } else {
                        $that.allocationOrderInInfo.totalMount = "请输入正确的数字";
                        throw Error();
                    }
                });
                $that.allocationOrderInInfo.totalMount = j.toFixed(2);
            },
            //查询仓库
            _selectShopHouses: function (shId) {
                var param = {
                    params: {
                        shopId: $that.allocationOrderInInfo.shopId,
                        shId: shId,
                        communityId: vc.getCurrentCommunity().communityId,
                        flag: "1",
                        page: 1,
                        row: 50
                    }
                };
                vc.http.apiGet('/shopHouse.queryShopHouse',
                    param,
                    function (json, res) {
                        var listShopHouseData = JSON.parse(json);
                        $that.allocationOrderInInfo.shopHouses = listShopHouseData.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    }
                );
            }
        }
    });
})(window.vc);
